<?php
namespace src\logic;
use classlib\core as C;
class lstripepay {
	/**
	 * 添加一张充值卡到UC
	 * @param $token
	 * @param $stripetoken
	 */
	public static function add_customer($token, $stripetoken) {
		$conf   = C\config::load('psocial', false);
		$qrystr = array('token'=>$token, 'stripeToken'=>$stripetoken, 'spotCd'=>lsocial::SpotCd);
		$url    = $conf['psapi'].'/payment/stripe/customer/?'.http_build_query($qrystr);
		$res    = lsocial::requst($url, 'POST');
		return $res;
	}
	
	/**
	 * 删除一张绑定的卡信息
	 * @param $customerid
	 * @param $token
	 */
	public static function del_customer($customerid, $token) {
		$conf   = C\config::load('psocial', false);
		$qrystr = array('token'=>$token, 'customerId'=>$customerid, 'spotCd'=>lsocial::SpotCd);
		$url    = $conf['psapi'].'/payment/stripe/customer/'.$customerid.'?'.http_build_query($qrystr);
		$res    = lsocial::requst($url, 'DELETE');
		return $res;
	}
	
	/**
	 *获取用户绑定的卡资料信息
	 * @param $customerid
	 * @param $token
	 */
	public static function get_customer($token) {
		$conf   = C\config::load('psocial', false);
		$qrystr = array('token'=>$token, 'spotCd'=>lsocial::SpotCd);
		$url    = $conf['psapi'].'/payment/stripe/customer/?'.http_build_query($qrystr);
		$res    = lsocial::requst($url, 'GET');
		return $res;
	}
	
	/**
	 * 直接通过Strip充值支付
	 * @param $customerid int
	 * @param $token  string
	 * @param $amount int
	 * @param $orderId string
	 */
	public static function charge($customerid, $token, $amount, $orderId) {
		$conf   = C\config::load('psocial', false);
		$qrystr = array('token'=>$token, 'customerId'=>$customerid, 'orderId'=>$orderId, 
			'amount'=>$amount, 'spotCd'=>lsocial::SpotCd, 'ts'=>lsocial::microsec());
		$qrystr['hs'] = md5($conf['skey'].$qrystr['ts'].$customerid.$amount);
		$url    = $conf['psapi'].'/payment/stripe/charge/?'.http_build_query($qrystr);
		$res    = lsocial::requst($url, 'POST');
		return $res;
	}
	
	public static function add_account($token, $country, $email, $firstname, $lastname, $documentid, $accountholdertype, $banktoken, $dob, $postalcode, $line1) {
		$conf   = C\config::load('psocial', false);
		$qrystr = array('token'=>$token, 'country'=>$country, 'email'=>$email, 'firstname'=>$firstname, 'lastname'=>$lastname,
				'documentid'=>$documentid, 'accountholdertype'=>$accountholdertype, 'banktoken'=>$banktoken, 'dob'=>$dob,
				'postalcode'=>$postalcode, 'line1'=>$line1,'spotCd'=>lsocial::SpotCd);
		$url    = $conf['psapi'].'/v2/payment/stripe/account/?'.http_build_query($qrystr);
		$res    = lsocial::requst($url, 'POST');
		return $res;
	}
	
	public static function update_account($token, $userStripeId, $country, $banktoken, $firstname, $lastname, $documentid, $dob, $postalcode, $line1, $status=0) {
		$conf   = C\config::load('psocial', false);
		$qrystr = array('token'=>$token, 'country'=>$country, 'banktoken'=>$banktoken, 'postalcode'=>$postalcode, 'line1'=>$line1, 'spotCd'=>lsocial::SpotCd);
		if ($status != 1) {
			$qrystr['firstname'] = $firstname;
			$qrystr['lastname']  = $lastname;
			if ($documentid) {
				$qrystr['documentid']= $documentid;
			}
			$qrystr['dob']       = $dob;
		}
		$url    = $conf['psapi'].'/v2/payment/stripe/account/'.$userStripeId.'/?'.http_build_query($qrystr);
		$res    = lsocial::requst($url, 'PUT');
		return $res;
	}
	
	public static function del_account($token, $userStripeId) {
		$conf   = C\config::load('psocial', false);
		$qrystr = array('token'=>$token, 'spotCd'=>lsocial::SpotCd);
		$url    = $conf['psapi'].'/v2/payment/stripe/account/'.$userStripeId.'/?'.http_build_query($qrystr);
		$res    = lsocial::requst($url, 'DELETE');
		return $res;
	}
	
	/**
	 * 提交Stripe转账逻辑 接口已经验收
	 * @param unknown $token
	 * @param unknown $amount
	 * @param unknown $plivePayoutId
	 */
	public static function payout($token, $amount, $plivePayoutId) {
		$conf   = C\config::load('psocial', false);
		$qrystr = array('token'=>$token, 'amount'=>$amount, 'currency'=>$conf['currency'], 
			'plivePayoutId'=>$plivePayoutId, 'spotCd'=>lsocial::SpotCd, 'ts'=>lsocial::microsec());
		$qrystr['hs'] = md5($conf['skey'].$qrystr['ts'].$amount.$plivePayoutId);
		$url    = $conf['psapi'].'/payment/stripe/payout/?'.http_build_query($qrystr);
		$res    = lsocial::requst($url, 'POST');
		return $res;
	}
	
	public static function document($file) {
		$conf   = C\config::load('psocial', false);
		$qrystr = array('file'=>$file, 'spotCd'=>lsocial::SpotCd);
		$url    = $conf['psapi'].'/payment/stripe/account/document/';
		$res    = lsocial::requst($url, 'POST', $qrystr);
		return $res;
	}
}